﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Helpers;
using SYcwpb.Models;
using System.Data;

namespace SYcwpb.Access
{
    class LoginAccess
    {
        private SqlDataLayer sqlHelper = new SqlDataLayer();

        public LoginResultType CheckUserExist(ref LoginModel loginModel)
        {
            LoginResultType result = LoginResultType.Success;

            string sql_checkUser = "select count(*) from [system_user] S left join Position P on S.user_post = P.ID where " 
                + "S.user_post ='" + loginModel.Depart + "' and S.user_name = '" + loginModel.UserID + "' and S.user_line = '" + loginModel.Line + "'";

            if ( Convert.ToInt32(sqlHelper.GetSingle(sql_checkUser)) > 0)
            {
                StringBuilder sb = GlobalVariables.DecryptCode(loginModel.PasWrd);
                string sql_checkPasWrd = "select S.user_view_name , P.post_name from [system_user] S left join Position P on S.user_post= P.Id where "
                    + "S.user_password = '" + sb.ToString() + "' and S.user_name = '" + loginModel.UserID + "'";
                DataTable dt = sqlHelper.SelectDt(sql_checkPasWrd);

                if (null != dt && dt.Rows.Count == 1)
                {
                    loginModel.PostName = dt.Rows[0]["post_name"].ToString();
                    loginModel.UserName = dt.Rows[0]["user_view_name"].ToString();
                }
                else
                    result = LoginResultType.PasWrdError;
            }
            else
                result = LoginResultType.NoOne;

            return result;
        }

        public DataTable GetUserHeadImgInfo(string line)
        {
            DataTable dt = null;

            string sql = "select user_name, user_headImgMD5, user_img from [system_user] where user_dep_id = 7 and user_line = " + line;
            dt = sqlHelper.SelectDt(sql);

            return dt;
        }
    }

    public enum LoginResultType
    {
        NoOne = 0,
        PasWrdError = 1,
        Success = 2,
    }
}
